package com.desaysv.hicar

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.location.*
import android.location.LocationManager
import android.os.Build
import android.os.Handler
import android.util.Log
import android.widget.Toast
import androidx.core.app.ActivityCompat
import java.util.*

object LocationManager {
    private lateinit var mContext: Context
    private lateinit var mLocationManager: LocationManager
    var mStartCount =0
    interface LocationCallBack{
        fun callBack(location:Location?)
        fun numCall(nums:Int)
    }
fun init(context:Context, type:Int, call: LocationCallBack){
        end()
        mContext =context
        mLocationManager = mContext.getSystemService(Context.LOCATION_SERVICE) as LocationManager
        var provider=LocationManager.GPS_PROVIDER
        when (type) {
            0 -> {
                provider=LocationManager.GPS_PROVIDER
            }
            1->{
                provider=LocationManager.NETWORK_PROVIDER
            }
            else -> {
                provider=LocationManager.PASSIVE_PROVIDER
            }
        }
        if (ActivityCompat.checkSelfPermission(
                mContext,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                mContext,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            Toast.makeText(mContext,"请先授予权限",Toast.LENGTH_SHORT).show()
            return
        }
        var lastLocation= mLocationManager.getLastKnownLocation(provider)
        if (lastLocation!=null) {
            call.callBack(lastLocation)
        }
        mLocationManager.requestLocationUpdates(provider,1000,1f,object:LocationListener{
            override fun onLocationChanged(location: Location) {
                call.callBack(location)
                mLocationManager.removeUpdates(this)
            }
        })

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            mLocationManager.registerGnssStatusCallback(
            object: GnssStatus.Callback() {
                override fun onStarted() {
                    super.onStarted()
                }

                override fun onStopped() {
                    super.onStopped()
                }

                override fun onFirstFix(ttffMillis: Int) {
                    super.onFirstFix(ttffMillis)
                }

                override fun onSatelliteStatusChanged(status: GnssStatus) {
                    super.onSatelliteStatusChanged(status)
                    call.numCall(status.satelliteCount)
                }
            }, Handler(mContext.mainLooper)
            )
        }else{
            mLocationManager.addGpsStatusListener(object :GpsStatus.Listener{
                @SuppressLint("MissingPermission")
                override fun onGpsStatusChanged(event: Int) {
                    var status= mLocationManager.getGpsStatus(null)
                    if(status==null){
                        call.numCall(0)
                        return
                    }
                    var max = status.maxSatellites
                    var stars=Collections.EMPTY_LIST
                    if(event==GpsStatus.GPS_EVENT_SATELLITE_STATUS){
                        var it = status.satellites.iterator()
                        var count =0
                        while (it.hasNext()&&count<=max){
                            var st = it.next()
                            stars.add(st)
                            count++
                        }
                        //获取实际的卫星数量
                        call.numCall(stars.size)
                    }

                }

            })
        }
}

    private fun end(){
        try {
            if(mLocationManager !=null){
                mLocationManager ==null
            }
        } catch (e: Exception) {
            e.printStackTrace()
            Log.d("LocationManager","还未初始化")
        }
    }

}